package com.hzlh.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzlh.dao.TCompanyBaseDao;
import com.hzlh.dao.TGeographyDao;
import com.hzlh.model.TCompanyBase;
import com.hzlh.model.TCompanyBaseVo;
import com.hzlh.model.TGeography;
import com.hzlh.service.IAIService;
import com.hzlh.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IAIServiceImpl implements IAIService {
    @Value("${server.port}")
    private String port;
    @Resource
    private TCompanyBaseDao tCompanyBaseDao;

    @Resource
    private TGeographyDao tGeographyDao;

    @Override
    public Map<String, List<Map<String,String>>> analysisCompanyFromMap() {
        Map<String, List<Map<String,String>>> resultMap = new HashMap<>();
        // 企业状态
        // "status1":{"续存":8,"注销":10,"吊销":10,"撤销":10,"迁出":10,"未知":12},
        List<Map<String,String>> innerList = new ArrayList<>(5);

        Map<String, String> statusMap1 = new HashMap<>();
        statusMap1.put("name","续存");
        statusMap1.put("value","10");
        innerList.add(statusMap1);

        Map<String, String> statusMap2 = new HashMap<>();
        statusMap2.put("name","注销");
        statusMap2.put("value","10");
        innerList.add(statusMap2);

        Map<String, String> statusMap3 = new HashMap<>();
        statusMap3.put("name","吊销");
        statusMap3.put("value","10");
        innerList.add(statusMap3);

        Map<String, String> statusMap4 = new HashMap<>();
        statusMap4.put("name","撤销");
        statusMap4.put("value","10");
        innerList.add(statusMap4);

        Map<String, String> statusMap5 = new HashMap<>();
        statusMap5.put("name","迁出");
        statusMap5.put("value","10");
        innerList.add(statusMap5);

        Map<String, String> statusMap6 = new HashMap<>();
        statusMap6.put("name","未知");
        statusMap6.put("value","10");
        innerList.add(statusMap6);
        resultMap.put("status", innerList);

        // 企业类型
        // "status2":{"有限责任公司":8,"责任有限公司":10,"国企":10,"外企":10,"独资企业":10,"合伙制企业":12},
        List<Map<String,String>> typeMapList = new ArrayList<>(5);

        Map<String, String> typeMap1 = new HashMap<>();
        typeMap1.put("name","有限责任公司");
        typeMap1.put("value","10");
        typeMapList.add(typeMap1);

        Map<String, String> typeMap2 = new HashMap<>();
        typeMap2.put("name","责任有限公司");
        typeMap2.put("value","10");
        typeMapList.add(typeMap2);

        Map<String, String> typeMap3 = new HashMap<>();
        typeMap3.put("name","国企");
        typeMap3.put("value","10");
        typeMapList.add(typeMap3);

        Map<String, String> typeMap4 = new HashMap<>();
        typeMap4.put("name","外企");
        typeMap4.put("value","10");
        typeMapList.add(typeMap4);

        Map<String, String> typeMap5 = new HashMap<>();
        typeMap5.put("name","独资企业");
        typeMap5.put("value","10");
        typeMapList.add(typeMap5);

        Map<String, String> typeMap6 = new HashMap<>();
        typeMap6.put("name","合伙制企业");
        typeMap6.put("value","10");
        typeMapList.add(typeMap6);
        resultMap.put("type", typeMapList);


        // 行业统计top5
        // "status3":{"批发和零售业":8,"租赁和商业服务业":10,"信息技术服务业":10,"住宿与餐饮业":10},
        List<Map<String,String>> industryMapList = new ArrayList<>(5);

        Map<String, String> industryMap1 = new HashMap<>();
        industryMap1.put("name","批发和零售业");
        industryMap1.put("value","10");
        industryMapList.add(industryMap1);

        Map<String, String> industryMap2 = new HashMap<>();
        industryMap2.put("name","租赁和商业服务业");
        industryMap2.put("value","10");
        industryMapList.add(industryMap2);

        Map<String, String> industryMap3 = new HashMap<>();
        industryMap3.put("name","信息技术服务业");
        industryMap3.put("value","10");
        industryMapList.add(industryMap3);

        Map<String, String> industryMap4 = new HashMap<>();
        industryMap4.put("name","住宿与餐饮业");
        industryMap4.put("value","10");
        industryMapList.add(industryMap4);
        resultMap.put("industryRank", industryMapList);

        // 年份统计top5
        // "status4":{"2019年":8,"2018年":10,"2017年":10,"2016年":10,"2015年":10,"2005年":12}}
        List<Map<String,String>> yearMapList = new ArrayList<>(5);

        Map<String, String> yearMap1 = new HashMap<>();
        yearMap1.put("name","2019年");
        yearMap1.put("value","10");
        yearMapList.add(yearMap1);

        Map<String, String> yearMap2 = new HashMap<>();
        yearMap2.put("name","2018年");
        yearMap2.put("value","10");
        yearMapList.add(yearMap2);

        Map<String, String> yearMap3 = new HashMap<>();
        yearMap3.put("name","2017年");
        yearMap3.put("value","10");
        yearMapList.add(yearMap3);

        Map<String, String> yearMap4 = new HashMap<>();
        yearMap4.put("name","2016年");
        yearMap4.put("value","10");
        yearMapList.add(yearMap4);

        Map<String, String> yearMap5 = new HashMap<>();
        yearMap5.put("name","2015年");
        yearMap5.put("value","10");
        yearMapList.add(yearMap5);

        Map<String, String> yearMap6 = new HashMap<>();
        yearMap6.put("name","2014年");
        yearMap6.put("value","10");
        yearMapList.add(yearMap6);
        resultMap.put("yearRank", yearMapList);
        return resultMap;
    }

    @Override
    public void transformCompanyLocation() {
        List<TCompanyBaseVo> list = tCompanyBaseDao.selectList(null);
//        List<TCompanyBase> list = new ArrayList<>(100);
        TCompanyBaseVo mode = null;
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                mode = list.get(i);
                if (StringUtils.isNotEmpty(mode.getAddress())) {
                    //String json = HttpUtil.get("http://127.0.0.1:"+port+"/api/syn", null);
                    String url = "http://api.map.baidu.com/geocoding/v3/?address=" + mode.getAddress() + "&output=json&ak=0kyBrwe87UQBKz4NbuhMm2duhcqRmxLj";
                    String json = HttpUtil.get(url, null);
                    JSONObject value = JSON.parseObject(json);
                    JSONObject resultValue = (JSONObject) value.get("result");
                    if (null != resultValue) {
                        JSONObject locationValue = (JSONObject) resultValue.get("location");
                        BigDecimal lng = (BigDecimal) locationValue.get("lng");
                        BigDecimal lat = (BigDecimal) locationValue.get("lat");

                        System.out.println("-----lng:" + lng);
                        System.out.println("-----lat:" + lat);
                        mode.setLocLat(lat);
                        mode.setLocLon(lng);
                        tCompanyBaseDao.update(mode);
                    }
                }
            }
        }
    }

    @Override
    public void transform() {
        List<TGeography> tGeographies = tGeographyDao.selectList(new TGeography());
        List<String> collect = tGeographies.stream().map(TGeography::getName).collect(Collectors.toList());
        collect.stream().forEach(v -> {
            String url = "http://api.map.baidu.com/geocoding/v3/?address=" + v + "&output=json&ak=0kyBrwe87UQBKz4NbuhMm2duhcqRmxLj";
            String json = HttpUtil.get(url, null);
            JSONObject value = JSON.parseObject(json);
            JSONObject resultValue = (JSONObject) value.get("result");
            if (null != resultValue) {
                JSONObject locationValue = (JSONObject) resultValue.get("location");
                BigDecimal lng = (BigDecimal) locationValue.get("lng");
                BigDecimal lat = (BigDecimal) locationValue.get("lat");
                TGeography tGeography = new TGeography();
                tGeography.setName(v);
                tGeography.setLocLat(lat);
                tGeography.setLocLon(lng);
                int i = tGeographyDao.updateArea(tGeography);
                System.out.println("-----name:" + v);
                System.out.println("-----lng:" + lng);
                System.out.println("-----lat:" + lat);
            }
        });
    }
}
